package com.jjc.android.util;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;
import androidx.core.os.ConfigurationCompat;


import com.hjq.toast.ToastUtils;
import com.jjc.android.GlobalManager;

import java.util.Locale;

/**
 * 多语言切换的帮助类
 * http://blog.csdn.net/finddreams
 */
public class MultiLanguageUtil {

    private static final String TAG = "MultiLanguageUtil";
    private static MultiLanguageUtil instance;
    private Context mContext;

    public static void init(Context mContext){
        if (instance == null) {
            synchronized (MultiLanguageUtil.class) {
                if (instance == null) {
                    instance = new MultiLanguageUtil(mContext);
                }
            }
        }
        instance.init();
    }

    public void init(){
        Locale targetLocale = getLanguageLocale();

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            wrap(mContext);
        }else{
            Configuration configuration = mContext.getResources().getConfiguration();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                configuration.setLocale(targetLocale);
            } else {
                configuration.locale = targetLocale;
            }
            configuration.setLayoutDirection(targetLocale);
            Resources resources = mContext.getResources();
            DisplayMetrics dm = resources.getDisplayMetrics();
            resources.updateConfiguration(configuration, dm);
        }
        if(GlobalActivityStack.INSTANCE.getCurrentActivity()!=null){
            GlobalActivityStack.INSTANCE.getCurrentActivity().recreate();
        }
    }



    public static MultiLanguageUtil getInstance() {

        return instance;
    }

    private MultiLanguageUtil(Context context) {
        this.mContext = context;
    }

    /**
     * 设置语言
     */
    public void setConfiguration() {
        Locale targetLocale = getLanguageLocale();
        Configuration configuration = mContext.getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(targetLocale);
        } else {
            configuration.locale = targetLocale;
        }
        Resources resources = mContext.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);//语言更换生效的代码!
    }

    public static final int LANGUAGE_ENGLISH = 0;
    public static final int LANGUAGE_CHINESE = 1;
    public static final int LANGUAGE_SYSTEM = 2;
    /**
     * 如果不是英文、简体中文、繁体中文，默认返回简体中文
     * @return
     */
    public Locale getLanguageLocale() {
        int languageId = getLanguageId();
        if (LANGUAGE_ENGLISH == languageId) {
            return Locale.ENGLISH;
        } else if (LANGUAGE_CHINESE == languageId) {
            return Locale.SIMPLIFIED_CHINESE;
        }
        return getSysLocale();
    }

    public int getLanguageType() {
        return getLanguageId();
    }

    public boolean isEnglish(){
        return "en".equals(getLanguageStr());
    }

    public String getStr(int id) {
        return mContext.getResources().getString(id);
    }

    public String getValue(String chinese,String english){
        return isEnglish()?english:chinese;
    }

    public String getLanguageStr() {
        Locale languageLocale = getLanguageLocale();
        if (languageLocale.getDisplayName().contains("En") || languageLocale.getDisplayName().contains("en") || languageLocale.getDisplayName().contains("英语")) {
            return "en";
        } else if (languageLocale.getLanguage().toLowerCase(Locale.ROOT).contains("zh")||Locale.SIMPLIFIED_CHINESE.equals(languageLocale)||Locale.TRADITIONAL_CHINESE.equals(languageLocale) || new Locale("zh-rHK").equals(languageLocale)) {
            return "zh-Hans";
        }
        return "en";
    }

    private String getSystemLanguage(Locale locale) {
        return locale.getLanguage() + "_" + locale.getCountry();
    }

    //以上获取方式需要特殊处理一下
    public Locale getSysLocale() {
        Locale locale = null;
        try {
            locale = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0);;
        }catch (Exception e){

        }
        if(locale==null){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                locale = LocaleList.getDefault().get(0);
            } else {
                locale = Locale.getDefault();
            }
        }
        return locale;
    }

    /**
     * 更新语言
     *
     * @param languageType
     */
    public void updateLanguage(int languageType) {
        setLanguageId(languageType);
        MultiLanguageUtil.getInstance().setConfiguration();
    }

    public Context attachBaseContext(Context context) {
        updateLocal(context,context.getResources());
        return context;
    }

    public ContextWrapper wrap(Context context) {
        updateLocal(context,context.getResources());
        return new ContextWrapper(context);
    }

    public void updateLocal(Context context,Resources res){
        Locale newLocale = getLanguageLocale();
        Configuration configuration = res.getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.setLocale(newLocale);
            LocaleList localeList = new LocaleList(newLocale);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);
            context = context.createConfigurationContext(configuration);
        } else {
            configuration.setLocale(newLocale);
            context = context.createConfigurationContext(configuration);
        }
        res.updateConfiguration(configuration,res.getDisplayMetrics());
    }


    public int getLanguageId() {
        return GlobalManager.INSTANCE.getLanguageId();
    }

    public void setLanguageId(int languageType) {
        GlobalManager.INSTANCE.setLanguageId(languageType);
    }


}
